#include "componentcall.h"
#include "htmlattributelist.h"
#include "htmlattribute.h"
#include "memberlist.h"
#include "component.h"
#include "errorlog.h"
#include "property.h"
#include "membertemplate.h"
#include "memberevent.h"

ComponentCall::ComponentCall(const QString& componentName, const QString& varName, HtmlAttributeList* attribs, MemberList* members, const YYLTYPE& loc)
        : Namespace(loc), m_componentName( componentName ), m_varName( varName ), m_attribs(attribs), m_members(members)
{
    appendChild(attribs);
    appendChild(members);
}

Node* ComponentCall::getEntry( const QString& name )
{
    if ( name == m_varName )
        return this;
    return 0;
}

void ComponentCall::resolveAll()
{
    this->Namespace::resolveAll();

    // Does a component of the specified name exist?
    m_component = dynamic_cast<Component*>( resolve(m_componentName) );
    if ( !m_component )
        ErrorLog::error( location(), "Unknown component " + m_componentName );

    // Does the component have all the required properties, templates etc.?
    foreach( HtmlAttribute* a, m_attribs->attributes() )
    {
        Node* p = m_component->getEntry(a->name());
        if ( !p )
            ErrorLog::error(a->location(), "The component " + m_component->name() + " has no property or event named " + a->name());
        else if ( !dynamic_cast<Property*>(p) && !dynamic_cast<MemberEvent*>(p) && !dynamic_cast<MemberTemplate*>(p) )
            ErrorLog::error(a->location(), "The member " + a->name() + " of component " + m_component->name() + " is neither a property, a variable template nor an event");
        else
            m_membersForAttribs.append(p);
    }

}

Type ComponentCall::type()
{
    return Type(Type::Scalar);
}
